<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   this($)*/

!defined('DYHB_PATH') && exit;

class TemplateGtkGladeWidget extends TemplateGtkGladeObj{

	private $_sClass;
	private $_sId;
	private $_arrSignals=array();

	public function __construct($sClass,$sId){
		A::STRING($sClass);
		A::STRING($sId);
		A::ASSERT_($sClass);
		A::ASSERT_($sId);
		$this->_sClass=$sClass;
		$this->_sId=$sId;
	}

	public function getWidgetClass(){
		return $this->_sClass;
	}

	public function getWidgetId(){
		return $this->_sId;
	}

	public function setWidgetId($sId){
		$this->_sId=$sId;
	}

	public function addTemplateObj(TemplateGtkGladeWidget $oWidget,TemplateGtkGladeWidgetPacking $oPacking){
		$this->_arrChildTemplateObj[]=new TemplateGtkGladeWidgetChild($oWidget,$oPacking);
	}

	public function clearChildWidget(){
		$nCount=$this->_arrChildTemplateObj;
		$this->_arrChildTemplateObj=array();
		return $nCount;
	}

	public function addSignal($sName, $sFunction, $sObject, $bAfter){
		$this->_arrSignals[]=array('name'=>$sName,'function' => $sFunction,'object'=>$sObject,'after'=>$bAfter);
	}


	public function createSignalIterator(){
		return new DArrayIterator($this->_arrSignals);
	}

	public function compile(){
		$oTemplate=$this->getTemplate();// 检查 同名 窗体是否存在
		$oTemplate->checkSameIdWidget_($this->getWidgetId(),$this->getWidgetClass());
		TemplateObj::compile();// 编译自己(窗体基本创建$oTemplate->代码)
		$oParser=$this->getParser();// Glade common 设定
		A::INSTANCE($oParser,'ITemplateObjParser');
		$oCompilerManager=$oParser->getCompilerManager();
		A::INSTANCE($oCompilerManager,'TemplateObjProcessorManager');
		$oCompiler=$oCompilerManager->getProcessor('CommonSetting');
		A::INSTANCE($oCompiler,'ITemplateObjCompiler');
		$oCompiler->compile($this);
		$oCompiler=$oCompilerManager->getProcessor('Signals');// Signals
		A::INSTANCE($oCompiler,'ITemplateObjCompiler');
		$oCompiler->compile($this);
		$sCompiled=(string)$this->getCompiled();// 已编译内容
		$sWidgetClass=$this->getWidgetClass();// widget
		$sWidgetId=$this->getWidgetId();
		$sVariable='$'.TemplateGtkGlade::getWidgetVariableName($this);
		$sFunctionName=TemplateGtkGlade::getWidgetCreateFunctionName($this);// 窗体创建函数
		$sVariableName='$'.TemplateGtkGlade::getWidgetVariableName($this);
		$sCompiled="
	/**
	 * 创建窗体 {$sWidgetClass}:{$sWidgetId}
	 */
	 protected function {$sFunctionName}(array \$arrObjectForSignal=array()){
		A::ISTHESE(\$arrObjectForSignal, array('array:object,null'),G::L('\$arrObjectForSignal必须为对象或者null','Dyhb'));
		foreach(\$arrObjectForSignal as \$sObName=>\$oObject){// 声明 用于响应 Signal 事件的对象的引用
			if(\$oObject)
				\$\$sObName=\$oObject;
		}
		{$sCompiled}
		if(isset({$sVariableName})and {$sVariableName}->get_name()=='{$sWidgetClass}'){// 窗体名称
			{$sVariableName}->set_name('{$sWidgetId}');
		}
		if(isset({$sVariableName})and !{$sVariableName}->get_data('creator')){// 在 窗体中 记录 Creator
			{$sVariableName}->set_data('creator',\$this);
		}
		return {$sVariableName};
	}

";
		$oTemplate->addWidget_($this->getWidgetClass().':'.$this->getWidgetId());// 设置 窗体树
		$oIterator=$this->createTemplateObjIterator();// 编译 子窗体
		$oIterator->first();
		while(!$oIterator->isDone()){
			$oChildTemplateObject=$oIterator->current();
			A::INSTANCE($oChildTemplateObject,'TemplateGtkGladeWidgetChild');
			$oChildWidget=$oChildTemplateObject->getWidget();
			A::INSTANCE($oChildTemplateObject,__CLASS__);
			$oTemplate->addWidget_($oChildTemplateObject->getWidgetClass().':'.$oChildTemplateObject->getWidgetId(),$this->getWidgetClass().':'.$this->getWidgetId());// 设置 窗体树
			$oChildWidget->compile();// 编译子对象
			$sCompiled.=$oChildWidget->getCompiled();// 置换 对象
			$oIterator->next();
		}
		$this->setCompiled($sCompiled);
	}

}
